package com.bagua.sbbcshop.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author x.hou 32位MD5加密算法
 */
public class JDKencMD5_32 {
	
	private static final Logger logger = LoggerFactory.getLogger(JDKencMD5_32.class);

	// 32位md5加密算法
	public static String getMD5Str(String str) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("gbk"));
		} catch (NoSuchAlgorithmException e) {
			logger.error("md5加密" + e);
		} catch (UnsupportedEncodingException e) {
			logger.error("32位md5加密" + e);
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
			} else {
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
		}
		return md5StrBuff.toString();
	}

}